package com.ytg.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;

import com.ytg.myapi.BindWay;
import com.ytg.myapi.Injector;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.ObservableArrayMap;
import androidx.viewbinding.ViewBinding;

/**
 * 作者： 于堂刚,
 * 日期： 2021/6/17.
 * 描述：
 */
public abstract class BaseAct<T extends Injector,V extends ViewBinding> extends AppCompatActivity {
    T injector;
    protected V vbd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ((T) BindWay.getInstance().getInjector(this)).init(this);
        super.onCreate(savedInstanceState);
        try {
            if(getClass().getGenericSuperclass() instanceof ParameterizedType){
                ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
                Type types[] = type.getActualTypeArguments();
                if(null!=types){
                    for (int i = 0; i <types.length ; i++) {
                        Class cls = (Class) type.getActualTypeArguments()[i];
                        if(cls.getName().contains("Binding")){
                            Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
                            vbd = (V) inflate.invoke(null, getLayoutInflater());
                            break;
                        }
                    }
                }
            }
            setContentView(vbd.getRoot());
            injector = ((T) BindWay.getInstance().bind(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void setData(String key, String data){
        injector.setData(key, data);
    }
    public String getData(String key){
        return injector.getData(key);
    }
    public ObservableArrayMap getData(){
      return  injector.getData();
    }
    public abstract  int getLayoutId();
}
